import request from '../../static/utils/request'
//函数节流使用
let isSend = false;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        placeholder:'',
        hotList:[], //热搜榜内容
        searchContent:'', //搜索内容
        searchList:[], //关键字模糊匹配数据
        historyList:[], //历史记录数组
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 初始化数据
        this.getInitData();
        // 获取历史记录
        this.getSearchHistoryList();
    },
    //获取初始化数据
    async getInitData(){
        let placeholder = await request('/search/default');
        let hotList = await request('/search/hot/detail');
        this.setData({
            placeholder:placeholder.data.showKeyword,
            hotList:hotList.data
        })
    },
    //从storage中获取历史记录
    getSearchHistoryList(){
        let historyList = wx.getStorageSync('searchHistory');
        // console.log(historyList);
        if(historyList){
            this.setData({
                historyList
            })
        }
    },

    //表单内容改变
    handleInputChange(event){
        let searchContent = event.detail.value.trim(); //trim()删除空白符
        this.setData({
            searchContent
        })

        if(isSend){
            return
        }
        isSend = true;

       this.getSearchList();
        //使用函数节流做性能优化，防止发送过多请求
        setTimeout(()=>{
            isSend = false;
            //发请求获取关键字模糊匹配
        },300)
        
    },

    //获取搜索数据
    async getSearchList(){
        if(!this.data.searchContent){
            this.setData({
                searchList:[]
            })
            return
        }
        let{searchContent,historyList} = this.data;
        let searchListData = await request('/search', {keywords: searchContent, limit: 10});
        //频繁搜索提示
        if(searchListData.code != '200'){
            wx.showToast({
                title: searchListData.msg,
                icon:'none'
            });
        }else{
            this.setData({
                searchList: searchListData.result.songs
            })
        }

        
        // 判断重复，直接删除重复的数组，后面再添加
        if(historyList.indexOf(searchContent) !== -1){
            historyList.splice(historyList.indexOf(searchContent), 1)
        }
        // 将搜索记录添加到历史中，且添加到数组头部 unshift()
        historyList.unshift(searchContent);
        this.setData({
            historyList
        })
        wx.setStorageSync('searchHistory', historyList);
          
    },
    clearSearchContent(){
        this.setData({
            searchContent:'',
            searchList:[]
        })
    },
    deleteHistory(){
        wx.showModal({
            content: '确定删除？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#7bbace',
            success: (result) => {
                if (result.confirm) {
                    //清空data中historyList
                    this.setData({
                        historyList:[]
                    })
                    wx.removeStorageSync('searchHistory');
                }
            },
            fail: () => {},
            complete: () => {}
        });
          
        
          
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})